import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import { message } from 'antd';
import { queryList, rePushOrder } from './service';

import { loginPath } from '../../../../../config/request.config';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        fetchPush: Effect;
    };
    reducers: {
        save: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'logisCenterLogList',

    state: {
        data: {
            list: [],
            count: 0,
        },
        detailInfo: {},
        printVisible: false,
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryList, payload);
            switch (response.code) {
                case 0:
                    yield put({
                        type: 'save',
                        payload: response.body,
                    });
                    break;
                case 1:
                    yield put({
                        type: 'save',
                        payload: { count: 0, list: [] },
                    });
                    message.error(response.msg);
                    break;
                case 6:
                    message.error(response.msg);
                    window.location.href = loginPath;
                    break;
                default:
                    message.error(response.msg);
            }
        },

        * fetchPush({ payload }, { call, put }) {
            const response = yield call(rePushOrder, payload);
            switch (response.code) {
                case 0:
                    message.success('补推成功!');
                    break;
                case 1:
                    message.error(response.msg);
                    break;
                case 6:
                    message.error(response.msg);
                    window.location.href = loginPath;
                    break;
                default:
                    message.error(response.msg);
            }
        }
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
    },
};

export default Model;
